String.prototype.trim=function(){
	return this.replace(/^\s*|\s*$/g, "");
};
$(document).ready(function(){
   $("a[type='tmpt']").live("click",function(){
	 if($(this).parent().parent().find("div.config").length>0)
	       return false;
	 var queryId = $(this).attr("datatypeid") ;
   	 var configDiv = paramConfigDiv(queryId);
   	 $(this).parent().after(configDiv);
   	 return false;
  });
  
  $("a.close").live("click",function(){
     $(this).parent().remove();
  }) ;
  
  $("input.browse").live("click",function(){
	  var element = $(this).parent().parent().find("a.title");
	  if(!(element.length>0))
		  element = $(this).parent().parent().find("a.instance-title");
	  var type = element.attr("type");
	  var id= element.attr("datatypeid") ;
	  var args = [];
	  $(this).parent().find(".arg").each(function(){
		  var arg={};
		  var pos =$(this).find(".argName").attr("pos");
		  var type = $(this).find(".argType").html().trim();
		  var value = $(this).find(".argValue").val().trim();
		  arg["pos"] = pos;
		  arg["type"] = type;
		  arg["value"] = value;
		  args.push(JSON.stringify(arg)) ;
	  });
	  if(type=="tmpt"){
		  var data ={};
		  data["id"] = id;
		  data["args"] = args;
		  data = JSON.stringify(data);
		  data = "type=tmpt&obj="+encodeURIComponent(data);
		  $.ajax({
				url:"answer",
				data:data,
				type:"post",
				dataType:"text",
				success:function(result){
				    if(result="ok")
				    	window.location= "answer.jsp";
				}
		  });
	  }
  });
  
});

function paramConfigDiv(queryId){
   var div = $("<div class='config'></div>") ;
   var aClose = $("<a href='#' class='close'></a>");
   var spanClose = $('<span  class="closeTag"></span>');
   aClose.append(spanClose);
   div.append(aClose) ;
   var h3  = $("<h3 class='config-title'>Example</h3>");
   div.append(h3);
    
	var exampleDiv = $("<div class='example'></div>");
	div.append(exampleDiv) ;
    getExample(queryId,exampleDiv);
    
   var argArray=getArgs(queryId);

   var h4  = $("<h3 class='config-title'>Arguments Configuration</h3>");
   div.append(h4);
   var divArgList =$('<div class="argList"></div>');
	 div.append(divArgList);
   var browseButton = $("<input class='browse' type='button' value='Browse' width=150 />");
   div.append(browseButton) ;
   getArgs(queryId,divArgList) ;
     return div;
}

function getExample(queryId,container){
	var example;
	var data='{"query_id":"'+queryId+'"}';
	data = "getExample="+encodeURIComponent(data);
	$.ajax({
		url:"manageQuery",
		data:data,
		type:"post",
		dataType:"text",
		success:function(result){
			container.html(result) ;
		}
	});
	return example;
}

function getArgs(queryId,divArgList){
	var data='{"query_id":"'+queryId+'"}';
	data = "getArgs="+encodeURIComponent(data);
	$.ajax({
		url:"manageQuery",
		data:data,
		type:"post",
		dataType:"json",
		success:function(result){
			var argArray  = result["result"];
			for(i=0;i<argArray.length;i++){
				var divArg=$('<div class="arg"></div>');
				divArgList.append(divArg) ;
				var arg = argArray[i];
				var argType =arg["type"] ;
				var argName =arg["name"];
				var argNameSpan =$('<span class="argName" ></span>').html(argName);
			  argNameSpan.addClass(argType) ; 
			  argNameSpan.attr("pos" ,i);
		    var textnode = document.createTextNode("	");  
				argNameSpan.append(textnode);
				divArg.append(argNameSpan);
				var typeSpan =$('<span class="argType" ></span>').html(argType);
				divArg.append(typeSpan);
	      var input = $("<input type='text' class='argValue'/>");
	      divArg.append(input);
	  }
		}
	});
}



